#-------------------------------------------------------------------------------
# @file 	CMakeLists
# @author 	mojinpan
# @copyright (c) 2018-2024 mojinpan. All rights reserved.
# @brief 	linux下app的CMake脚本
# @version 	V0.3
# @date 	2024-08-27
#
# @brief 	功能说明
# @details
# 1.
# 
# @version 	V0.1
# @date 	2024-08-10
# @details
# 1.实现按文件夹命名bin文件,并指定在根目录生成
# 2.实现含静态库的编译
# 3.实现编译器,编译参数等编译参数的自定义配置
# 4.实现debug模式和调试模式的动态配置
#
# @version 	V0.2
# @date 	2024-08-12
# @details
# 1.修正静态库和动态库列表变量没有传递给子cmake的bug
#
# @version 	V0.3
# @date 	2024-08-27
# @details
# 1.增加子CMake的库链接路径和头文件查找路径变量
#-------------------------------------------------------------------------------
# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.10)
# 获取当前目录的名称
get_filename_component(CMAKE_PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR}.elf NAME)

# 静态库清单
set(STATIC_LIB_LIST "")
# 动态库清单
set(SHARED_LIB_LIST "")
# 库文件路径清单
set(LIB_LINK_DIR_LIST "")
# 头文件路径清单
set(LIB_INCLUDE_DIR_LIST "")
#-------------------------------------------------------------------------------
# 工程定义项
#-------------------------------------------------------------------------------
# 项目名称
project(${CMAKE_PROJECT_NAME})

#设置编译器
set (PREFIX  "arm-linux-gnueabihf-")

#第三方动态链接库
set(LdFlags "-lpthread -lrt -ldl")

# 指定库文件目录
add_subdirectory(${PROJECT_SOURCE_DIR}/src/Lib/Sqlite3)
add_subdirectory(${PROJECT_SOURCE_DIR}/src/Lib/mosquitto)
add_subdirectory(${PROJECT_SOURCE_DIR}/src/Lib/tbox/)

# 指定源文件目录
file(GLOB APP_SRC 
    ${PROJECT_SOURCE_DIR}/src/App/*.c 
    ${PROJECT_SOURCE_DIR}/src/App/*.cpp
    ${PROJECT_SOURCE_DIR}/src/Bas/*.c 
    ${PROJECT_SOURCE_DIR}/src/Bas/*.cpp
    ${PROJECT_SOURCE_DIR}/src/Hal/*.c 
    ${PROJECT_SOURCE_DIR}/src/Hal/*.cpp
)
list(APPEND SRC ${APP_SRC})
# 库相关源文件独立添加
aux_source_directory(${PROJECT_SOURCE_DIR}/src/Lib/json SRC)

# 指定头文件目录
include_directories(
    ${LIB_INCLUDE_DIR_LIST}
    ${PROJECT_SOURCE_DIR}/src/App
    ${PROJECT_SOURCE_DIR}/src/Bas
    ${PROJECT_SOURCE_DIR}/src/Hal
    ${PROJECT_SOURCE_DIR}/src/Lib/Inc
    ${PROJECT_SOURCE_DIR}/src/Lib/json
    ${PROJECT_SOURCE_DIR}/src/Lib/lightmodbus
)
#-------------------------------------------------------------------------------
# 编译定义项
#-------------------------------------------------------------------------------
# 显示编译过程信息
set(CMAKE_VERBOSE_MAKEFILE OFF)

# 调试信息
set(DEBUG OFF)

# 设置编译器版本
# set(CMAKE_C_STANDARD   99)
set(CMAKE_CXX_STANDARD 17)

# 编译器配置
set (CMAKE_C_COMPILER   ${PREFIX}g++)
set (CMAKE_CXX_COMPILER ${PREFIX}g++)

# 编译参数配置
list(APPEND CMAKE_C_FLAGS   "-std=c++17 -Wall")
list(APPEND CMAKE_CXX_FLAGS "-std=c++17 -Wall -Wno-psabi -Wliteral-suffix")

# debug选项配置
if(DEBUG)
    add_compile_options(-g -ggdb -rdynamic)
else()
    add_compile_options(-O2 -s)
endif()

# 指定bin文件输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})

# 添加库路径
link_directories(${LIB_LINK_DIR_LIST})

# 定义可执行文件的名称和源文件
add_executable(${PROJECT_NAME} ${SRC})

# 添加链接库
target_link_libraries(${PROJECT_NAME} PRIVATE ${STATIC_LIB_LIST} ${SHARED_LIB_LIST} ${LdFlags})
